﻿using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace K9Cloud.Admin.Data.Migrations.K9CloudAdminDb
{
    public partial class InitAdmin : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "app_audit_log",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    Event = table.Column<string>(maxLength: 255, nullable: false),
                    EventId = table.Column<string>(maxLength: 255, nullable: false),
                    Data = table.Column<string>(type: "JSON", nullable: false),
                    UserId = table.Column<string>(nullable: true),
                    UserFullName = table.Column<string>(maxLength: 50, nullable: true),
                    TenantId = table.Column<Guid>(nullable: true),
                    ApiName = table.Column<string>(maxLength: 50, nullable: false),
                    AuditTime = table.Column<DateTimeOffset>(nullable: false),
                    Status = table.Column<int>(nullable: false),
                    Url = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_audit_log", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "app_organization_unit",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    LastModifierUserId = table.Column<Guid>(nullable: true),
                    LastModificationTime = table.Column<DateTimeOffset>(nullable: true),
                    DeleterUserId = table.Column<Guid>(nullable: true),
                    DeletionTime = table.Column<DateTimeOffset>(nullable: true),
                    IsDeleted = table.Column<bool>(nullable: false),
                    TenantId = table.Column<Guid>(nullable: true),
                    ParentId = table.Column<Guid>(nullable: true),
                    Code = table.Column<string>(maxLength: 95, nullable: false),
                    DisplayName = table.Column<string>(maxLength: 128, nullable: false),
                    Type = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_organization_unit", x => x.Id);
                    table.ForeignKey(
                        name: "FK_app_organization_unit_app_organization_unit_ParentId",
                        column: x => x.ParentId,
                        principalTable: "app_organization_unit",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "app_role_resource",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Api = table.Column<string>(maxLength: 50, nullable: false),
                    Role = table.Column<string>(maxLength: 100, nullable: false),
                    Resource = table.Column<string>(maxLength: 100, nullable: false),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    TenantId = table.Column<Guid>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_role_resource", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "app_setting",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(maxLength: 255, nullable: true),
                    Value = table.Column<string>(maxLength: 255, nullable: true),
                    TenantId = table.Column<Guid>(nullable: true),
                    UserId = table.Column<Guid>(nullable: true),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    LastModifierUserId = table.Column<Guid>(nullable: true),
                    LastModificationTime = table.Column<DateTimeOffset>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_setting", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "app_tenant",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    LastModifierUserId = table.Column<Guid>(nullable: true),
                    LastModificationTime = table.Column<DateTimeOffset>(nullable: true),
                    DeleterUserId = table.Column<Guid>(nullable: true),
                    DeletionTime = table.Column<DateTimeOffset>(nullable: true),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 255, nullable: true),
                    DisplayName = table.Column<string>(maxLength: 255, nullable: true),
                    EditionId = table.Column<int>(nullable: false),
                    LogoUrl = table.Column<string>(maxLength: 200, nullable: true),
                    IsActive = table.Column<bool>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_tenant", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "app_user_black_list",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    User = table.Column<string>(maxLength: 100, nullable: false),
                    Reason = table.Column<string>(maxLength: 200, nullable: false),
                    Policy = table.Column<int>(nullable: false),
                    Clients = table.Column<string>(nullable: true),
                    TenantId = table.Column<Guid>(nullable: true),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_user_black_list", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "app_user_weixin_login",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    UserId = table.Column<Guid>(nullable: false),
                    AppId = table.Column<string>(maxLength: 50, nullable: false),
                    UnionId = table.Column<string>(maxLength: 50, nullable: true),
                    OpenId = table.Column<string>(maxLength: 50, nullable: false),
                    ClientId = table.Column<string>(nullable: false),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_user_weixin_login", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_role",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    Name = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
                    ConcurrencyStamp = table.Column<string>(nullable: true),
                    IsDeleted = table.Column<bool>(nullable: false),
                    DeleterUserId = table.Column<Guid>(nullable: true),
                    DeletionTime = table.Column<DateTimeOffset>(nullable: true),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    TenantId = table.Column<Guid>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_role", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_user",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    UserName = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
                    Email = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
                    EmailConfirmed = table.Column<bool>(nullable: false),
                    PasswordHash = table.Column<string>(nullable: true),
                    SecurityStamp = table.Column<string>(nullable: true),
                    ConcurrencyStamp = table.Column<string>(nullable: true),
                    PhoneNumber = table.Column<string>(nullable: true),
                    PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                    TwoFactorEnabled = table.Column<bool>(nullable: false),
                    LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                    LockoutEnabled = table.Column<bool>(nullable: false),
                    AccessFailedCount = table.Column<int>(nullable: false),
                    CreatorUserId = table.Column<Guid>(nullable: true),
                    CreationTime = table.Column<DateTimeOffset>(nullable: false),
                    LastModifierUserId = table.Column<Guid>(nullable: true),
                    LastModificationTime = table.Column<DateTimeOffset>(nullable: true),
                    IsDeleted = table.Column<bool>(nullable: false),
                    DeleterUserId = table.Column<Guid>(nullable: true),
                    DeletionTime = table.Column<DateTimeOffset>(nullable: true),
                    TenantId = table.Column<Guid>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_user", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_role_claim",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    RoleId = table.Column<Guid>(nullable: false),
                    ClaimType = table.Column<string>(nullable: true),
                    ClaimValue = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_role_claim", x => x.Id);
                    table.ForeignKey(
                        name: "FK_asp_net_role_claim_asp_net_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "asp_net_role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_user_claim",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    UserId = table.Column<Guid>(nullable: false),
                    ClaimType = table.Column<string>(nullable: true),
                    ClaimValue = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_user_claim", x => x.Id);
                    table.ForeignKey(
                        name: "FK_asp_net_user_claim_asp_net_user_UserId",
                        column: x => x.UserId,
                        principalTable: "asp_net_user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_user_login",
                columns: table => new
                {
                    LoginProvider = table.Column<string>(nullable: false),
                    ProviderKey = table.Column<string>(nullable: false),
                    ProviderDisplayName = table.Column<string>(nullable: true),
                    UserId = table.Column<Guid>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_user_login", x => new { x.LoginProvider, x.ProviderKey });
                    table.ForeignKey(
                        name: "FK_asp_net_user_login_asp_net_user_UserId",
                        column: x => x.UserId,
                        principalTable: "asp_net_user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_user_role",
                columns: table => new
                {
                    UserId = table.Column<Guid>(nullable: false),
                    RoleId = table.Column<Guid>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_user_role", x => new { x.UserId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_asp_net_user_role_asp_net_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "asp_net_role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_asp_net_user_role_asp_net_user_UserId",
                        column: x => x.UserId,
                        principalTable: "asp_net_user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "asp_net_user_token",
                columns: table => new
                {
                    UserId = table.Column<Guid>(nullable: false),
                    LoginProvider = table.Column<string>(nullable: false),
                    Name = table.Column<string>(nullable: false),
                    Value = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_asp_net_user_token", x => new { x.UserId, x.LoginProvider, x.Name });
                    table.ForeignKey(
                        name: "FK_asp_net_user_token_asp_net_user_UserId",
                        column: x => x.UserId,
                        principalTable: "asp_net_user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "idx_tenant_time",
                table: "app_audit_log",
                columns: new[] { "TenantId", "AuditTime" });

            migrationBuilder.CreateIndex(
                name: "idx_tenant_user",
                table: "app_audit_log",
                columns: new[] { "TenantId", "UserId" });

            migrationBuilder.CreateIndex(
                name: "IX_app_organization_unit_ParentId",
                table: "app_organization_unit",
                column: "ParentId");

            migrationBuilder.CreateIndex(
                name: "idx_tenant_role_res",
                table: "app_role_resource",
                columns: new[] { "TenantId", "Api", "Role", "Resource" });

            migrationBuilder.CreateIndex(
                name: "idx_tenant_user",
                table: "app_setting",
                columns: new[] { "TenantId", "UserId" });

            migrationBuilder.CreateIndex(
                name: "idx_name",
                table: "app_tenant",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_user",
                table: "app_user_black_list",
                column: "User");

            migrationBuilder.CreateIndex(
                name: "idx_user_login",
                table: "app_user_weixin_login",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "idx_app_user",
                table: "app_user_weixin_login",
                columns: new[] { "ClientId", "AppId", "OpenId", "UnionId" });

            migrationBuilder.CreateIndex(
                name: "RoleNameIndex",
                table: "asp_net_role",
                column: "NormalizedName",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_tenant_role",
                table: "asp_net_role",
                columns: new[] { "TenantId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_asp_net_role_claim_RoleId",
                table: "asp_net_role_claim",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "EmailIndex",
                table: "asp_net_user",
                column: "NormalizedEmail");

            migrationBuilder.CreateIndex(
                name: "UserNameIndex",
                table: "asp_net_user",
                column: "NormalizedUserName",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_asp_net_user_UserName",
                table: "asp_net_user",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "idx_tenant_user",
                table: "asp_net_user",
                columns: new[] { "TenantId", "UserName" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_asp_net_user_claim_UserId",
                table: "asp_net_user_claim",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_asp_net_user_login_UserId",
                table: "asp_net_user_login",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_asp_net_user_role_RoleId",
                table: "asp_net_user_role",
                column: "RoleId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "app_audit_log");

            migrationBuilder.DropTable(
                name: "app_organization_unit");

            migrationBuilder.DropTable(
                name: "app_role_resource");

            migrationBuilder.DropTable(
                name: "app_setting");

            migrationBuilder.DropTable(
                name: "app_tenant");

            migrationBuilder.DropTable(
                name: "app_user_black_list");

            migrationBuilder.DropTable(
                name: "app_user_weixin_login");

            migrationBuilder.DropTable(
                name: "asp_net_role_claim");

            migrationBuilder.DropTable(
                name: "asp_net_user_claim");

            migrationBuilder.DropTable(
                name: "asp_net_user_login");

            migrationBuilder.DropTable(
                name: "asp_net_user_role");

            migrationBuilder.DropTable(
                name: "asp_net_user_token");

            migrationBuilder.DropTable(
                name: "asp_net_role");

            migrationBuilder.DropTable(
                name: "asp_net_user");
        }
    }
}
